package graphical.fatclient;

import java.awt.Dimension;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class Mainwindow extends Application{
	
	private static final Logger logger  = LoggerFactory.getLogger(Mainwindow.class);
	
	private static final String TITLE = "Bull-Dozer";
	private static final Dimension BASE_DIMENSION = new Dimension(800,600);

	private static StackPane root;
	private static Stage stage;
	private static MenuBar menuBar;
	private static VBox vbox;
	
	public static void launchWindow() {
		Mainwindow.launch();
	}

	@Override
	public void start(Stage primeStage) throws Exception {
		logger.trace("Window is starting.");
		stage = primeStage;
		initConfig();
		placeComponents();
		logger.trace("Windows is created.");
	}
	
	private void initConfig() {
		stage.setTitle(TITLE);
		stage.setScene(new Scene(getVBox(), BASE_DIMENSION.width, BASE_DIMENSION.height));
		stage.show();
	}
	
	private void placeComponents() {
		Text sampleText = new Text("To be completed!");
		sampleText.setFont(Font.font("Consolas", FontWeight.NORMAL, 40));
		getRoot().getChildren().add(sampleText);
		createMenuBar();
	}
	
	
	private static void createMenuBar() {
		Menu menuFile = new Menu("File");
		MenuItem quitItem = new MenuItem("Quit");
		quitItem.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent arg0) {
				Mainwindow.getStage().close();
			}
		});
		menuFile.getItems().add(quitItem);
		Menu menuHelp = new Menu("Help");
		getMenuBar().getMenus().addAll(menuFile, menuHelp);
	}
	
	private static StackPane getRoot() {
		if (root==null) {
			root = new StackPane();
		}
		return root;
	}

	private static MenuBar getMenuBar() {
		if (menuBar == null) {
			menuBar = new MenuBar();
		}
		return menuBar;
	}
	
	private static VBox getVBox() {
		if(vbox==null) {
			vbox = new VBox();
			getVBox().getChildren().add(getMenuBar());
			getVBox().getChildren().add(getRoot());
			VBox.setVgrow(getRoot(), Priority.ALWAYS);
		}
		return vbox;
	}
	
	private static Stage getStage() {
		return stage;
	}
}
